package cz.proks.easyhelpdesk.gwt.ui;

import java.util.Collection;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Widget;

import cz.proks.easyhelpdesk.api.model.Category;
import cz.proks.easyhelpdesk.gwt.event.CancelEvent;
import cz.proks.easyhelpdesk.gwt.event.TicketSaveEvent;
import cz.proks.easyhelpdesk.gwt.ui.composite.RestrictedHelpdeskComposite;
import cz.proks.easyhelpdesk.gwt.ui.layout.HelpdeskMainLayoutPanel;
import cz.proks.easyhelpdesk.gwt.ui.widget.TicketWidget;

public class NewTicketViewImpl extends RestrictedHelpdeskComposite implements NewTicketView, TicketSaveEvent.Handler, CancelEvent.Handler {

    private static TicketViewImplUiBinder uiBinder = GWT.create(TicketViewImplUiBinder.class);

    interface TicketViewImplUiBinder extends UiBinder<Widget, NewTicketViewImpl> {
    }
    
    private Presenter presenter;
            
    @UiField HelpdeskMainLayoutPanel mainHelpdeskPanel;
    @UiField TicketWidget ticketWidget;            

    public NewTicketViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));
    }
    
    @Override
    protected void initWidget(Widget widget) {
        super.initWidget(widget);
        
        ticketWidget.addTicketSaveHandler(this);
        ticketWidget.addCancelHandler(this);
    }                                

    @Override
    protected HelpdeskMainLayoutPanel getRootPanel() {
        return mainHelpdeskPanel;
    }    
    
    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public Category getCategory() {        
        Category category = null;
        
        if (ticketWidget.getCategoryId() != null && !ticketWidget.getCategoryId().equals(-1l)) {
            category = new Category();
            
            category.setId(ticketWidget.getCategoryId());
            category.setLabel(ticketWidget.getCategoryLabel());
        }
                
        return category;
    }
        

    @Override
    public void setCategories(Collection<Category> categories) {
        ticketWidget.setCategories(categories);
    }

    @Override
    public void setCategories(Category... categories) {
        ticketWidget.setCategories(categories);
    }

    @Override
    public String getSubject() {
        return ticketWidget.getSubject();
    }

    @Override
    public void setSubject(String subject) {
        ticketWidget.setSubject(subject);
    }

    @Override
    public String getDescription() {
        return ticketWidget.getDescription();
    }

    @Override
    public void setDescription(String description) {
        ticketWidget.setDescription(description);
    }

    @Override
    public void onTicketSave(TicketSaveEvent event) {
        if (presenter != null)
            presenter.saveTicket(event.getTicket());
    }            
    
    @Override
    public void onCancel(CancelEvent event) {
        if (presenter != null)
            presenter.cancel();
    }
    
    @Override
    public void onErase() {     
        super.onErase();
        
        ticketWidget.onErase();
    }
}
